package com.allen.study.common.exception;

import cn.hutool.extra.mail.MailException;
import com.allen.study.common.base.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.SuspendedEntityInteractionException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @description: 异常处理器
 * @author: AllenSun
 * @create: 2022-12-23
 **/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    // @ExceptionHandler(LeaveApprovalException.class)
    // public ResponseEntity<Map<String, Object>> handleLeaveApprovalException(LeaveApprovalException ex) {
    //     Map<String, Object> response = new HashMap<>();
    //     response.put("code", ex.getCode());
    //     response.put("message", ex.getMessage());
    //     return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
    // }

    // @ExceptionHandler(Exception.class)
    // public ResponseEntity<Map<String, Object>> handleGeneralException(Exception ex) {
    //     Map<String, Object> response = new HashMap<>();
    //     response.put("code", 500);
    //     response.put("message", "系统内部错误，请稍后重试");
    //     return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
    // }

    // @ExceptionHandler(NullPointerException.class)
    // public ApiResponse<String> nullPointerExceptionHandler(NullPointerException exception) {
    //     log.error("业务异常",exception);
    //     return ApiResponse.fail(ServiceExceptionEnum.SERVE_EXCEPTION.getDesc());
    // }
    //
    // @ExceptionHandler(ServiceInvokeException.class)
    // public ApiResponse<String> serviceInvokeExceptionHandler(ServiceInvokeException exception){
    //     log.error("业务异常",exception);
    //     return ApiResponse.fail(exception.getMessage());
    // }

    @ExceptionHandler(CustomRuntimeException.class)
    public ApiResponse<String> serviceInvokeExceptionHandler(CustomRuntimeException exception){
        log.error("业务异常",exception);
        return ApiResponse.fail(exception.getMessage());
    }

    @ExceptionHandler(CamundaProcessException.class)
    public ApiResponse<String> serviceInvokeExceptionHandler(CamundaProcessException exception){
        log.error("流程异常",exception);
        return ApiResponse.fail(exception.getMessage());
    }

    @ExceptionHandler(SuspendedEntityInteractionException.class)
    public ApiResponse<String> suspendedExceptionHandler(SuspendedEntityInteractionException exception){
        return ApiResponse.fail(ExceptionCode.INTERNAL_SERVER_ERROR.getMessage());
    }

    @ExceptionHandler(MailException.class)
    public ApiResponse<String> mailExceptionHandler(MailException exception){
        return ApiResponse.fail(exception.getMessage());
    }
}
